package com.eyeem.recyclerviewtools.sample;

import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.alexvasilkov.android.commons.texts.SpannableBuilder;
import com.alexvasilkov.android.commons.utils.Views;
import com.eyeem.recyclerviewtools.sample.data.Data;
import com.eyeem.recyclerviewtools.sample.gestureview.adapter.PaintingsAnimatorPagerAdapter;
import com.eyeem.recyclerviewtools.sample.gestureview.interface_.OnGesturePhotoListener;
import com.eyeem.recyclerviewtools.sample.gestureview.utils.GestureSettingsMenu;
import com.eyeem.recyclerviewtools.sample.widget.ExpandableTextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ImageBroswerAnimatorActivity extends BaseActivity implements
        ViewPager.OnPageChangeListener, OnGesturePhotoListener {

    private List<Data> paintings;

    public final static String BroswerImage = "BroswerImage";
    private BroswerImage mBroswerImage;
    private ViewPager viewPager;
    //private TextView titleView;
    private ExpandableTextView titleView;
    private TextView index;

    private GestureSettingsMenu settingsMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_viewpager_broswer);
        setTitle(null);
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        Toolbar toolbar = Views.find(this, R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //paintings = PaintingsHelper.list(getResources());
        mBroswerImage = getIntent().getParcelableExtra(BroswerImage);
        //paintings = DataAdapter.generateRandom(this).data;
        paintings = mBroswerImage.getPaintings();

        settingsMenu = new GestureSettingsMenu();
        settingsMenu.onRestoreInstanceState(savedInstanceState);

        titleView = Views.find(this, R.id.painting_title);
        index = Views.find(this, R.id.index);

        viewPager = Views.find(this, R.id.paintings_view_pager);
        Data[] imageArray = new Data[paintings.size()];
        viewPager.setAdapter(new PaintingsAnimatorPagerAdapter(viewPager, paintings.toArray(imageArray), settingsMenu, this));
        viewPager.addOnPageChangeListener(this);
        viewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.view_pager_margin));
        viewPager.setCurrentItem(mBroswerImage.getPosition());
        onPageSelected(0); // Manually calling for first item
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        settingsMenu.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return settingsMenu.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (settingsMenu.onOptionsItemSelected(item)) {
            invalidateOptionsMenu();
            viewPager.getAdapter().notifyDataSetChanged();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // no-op
    }

    @Override
    public void onPageSelected(int position) {
        CharSequence title = new SpannableBuilder(this)
                .createStyle().setFont(Typeface.DEFAULT_BOLD).apply()
                .append(paintings.get(position).text).append("\n")
                .clearStyle()
                .append(paintings.get(position).name)
                .build();
        titleView.setText(title);
        String text = (position + 1) + "/" + viewPager.getAdapter().getCount();
        //String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl("http://ggqu44by9bcdkxhbkbt.exp.bcedocument.com/download/doc-ghwku5hj3pb40cb.pdf&fileRealName=（教材）人教版语文四年级下册第1课——阅读：修辞手法突出景物特点.pdf");
        index.setText(text);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // no-op
    }

    @Override
    public void onGesturePhotoClick(int position) {
        onBackPressed();
    }

    public static final class BroswerImage implements Serializable, Parcelable {
        private ArrayList<Data> paintings;
        private int position;

        public ArrayList<Data> getPaintings() {
            return paintings;
        }

        public void setPaintings(ArrayList<Data> paintings) {
            this.paintings = paintings;
        }

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeTypedList(this.paintings);
            dest.writeInt(this.position);
        }

        public BroswerImage() {
        }

        protected BroswerImage(Parcel in) {
            this.paintings = in.createTypedArrayList(Data.CREATOR);
            this.position = in.readInt();
        }

        public static final Creator<BroswerImage> CREATOR = new Creator<BroswerImage>() {
            @Override
            public BroswerImage createFromParcel(Parcel source) {
                return new BroswerImage(source);
            }

            @Override
            public BroswerImage[] newArray(int size) {
                return new BroswerImage[size];
            }
        };
    }

}
